<?php

declare(strict_types=1);

namespace App\CustHub\Model;

use App\CustHub\Constants\QuestionnaireAnswerConstants;
use Mine\MineModel;

/**
 * @property int $id 主键
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property int $corp_id 企业id
 * @property int $robot_id 机器人id
 * @property int $customer 是否是客户
 * @property string $nickname 
 * @property string $avatar 头像
 * @property int $questionnaire_id 问卷id
 * @property int $item_id 问题id
 * @property int $option_id 选项id
 * @property string $content 回答内容
 * @property int $source 1通用链接  2专属链接  3高级群发-通用链接  4高级群发-专属链接
 * @property string $open_id openid
 * @property int $contact_id 联系人id
 */
class ChSmartQuestionnaireAnswer extends MineModel
{
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'ch_smart_questionnaire_answer';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'created_at', 'updated_at', 'corp_id', 'robot_id', 'customer', 'nickname', 'avatar', 'questionnaire_id', 'item_id', 'option_id', 'content', 'source', 'open_id', 'contact_id'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'corp_id' => 'integer', 'robot_id' => 'integer', 'customer' => 'integer', 'questionnaire_id' => 'integer', 'item_id' => 'integer', 'option_id' => 'integer', 'source' => 'integer', 'contact_id' => 'integer'];

    public static function answer($uid,$open_id,$corp_id,$robot_id,$nickname,$avatar,$item_id,$content = '',$is_customer = true)
    {
        $questionnaireItem = ChSmartQuestionnaireItem::query() -> where('id',$item_id) ->  first();
        self::query() -> updateOrCreate([
           'open_id' => $open_id,
           'item_id' => $item_id,
           'questionnaire_id' => $questionnaireItem['questionnaire_id'],
        ],[
            'corp_id' => $corp_id,
            'option_id' => in_array($questionnaireItem['question_type'],QuestionnaireAnswerConstants::getOptionTypes())?$content:null,
            'robot_id' => $robot_id,
            'uid' => $uid,
            'nickname' => $nickname,
            'avatar' => $avatar,
            'item_id' => $item_id,
            'content' => $content,
            'customer' => $is_customer?1:0,
        ]);
    }

    public static function branchAnswer($contact_id,$open_id,$corp_id,$robot_id,$questionnaire_id,$nickname,$avatar,$source,$data = [],$is_customer = true)
    {
        $insertData = [];
        foreach ($data as $item){
            $insertData[] = [
                'open_id' => $open_id,
                'item_id' => $item['id'],
                'option_id' => in_array($item['question_type'],QuestionnaireAnswerConstants::getOptionTypes())?$item['option_id']:0,
                'questionnaire_id' => $questionnaire_id,
                'corp_id' => $corp_id,
                'source' => $source,
                'robot_id' => $robot_id,
                'contact_id' => $contact_id,
                'nickname' => $nickname,
                'avatar' => $avatar,
                'created_at' => datetime(time()),
                'updated_at' => datetime(time()),
                'content' => $item['content'],
                'customer' => $is_customer?1:0,
            ];
            //更新选择题需统计数据
            if(in_array($item['question_type'],QuestionnaireAnswerConstants::getOptionTypes())){
                ChSmartQuestionnaireOption::where('id',$item['option_id']) -> increment('select_total');
            }
        }
        return self::insert($insertData);
    }
}
